dojo.declare("DCustomerOrderSelectPg", wm.Page, {
    orderQtyArr: [],
    start: function() {
        this.vOrderedItems.clearData();


    },
    "preferredDevice": "desktop",

    isNumber: function(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    },
    bOrderPreviewClick: function(inSender) {

        console.error("order array::" + this.orderQtyArr);
        console.error("Ordered items::" + this.vOrderedItems.getData());

        var data = this.vOrderedItems.getData();

        for (var i = 0; i < this.orderQtyArr.length; i++) {
            var itemQty = this.orderQtyArr[i];
            var itemId = itemQty.substr(0, itemQty.indexOf("#"));
            var selQty = itemQty.substr(itemQty.indexOf("#") + 1);
            // alert("looping out");
            for (var row = 0; row < data.length; row++) {
                var rowObj = data[row];
                var itemSel = rowObj.itemId;
                if (itemSel == itemId) {
                    // alert(itemId + "==match:itemSel=" + itemSel);
                    rowObj.quantity = selQty;
                    console.error("itemSel=" + itemSel + ";rowObj.itemName=" + rowObj.itemName + ";selQty=" + selQty + ";rowObj.orderedPrice=" + rowObj.orderedPrice + ";rowObj.itemDesc=" + rowObj.itemDesc);
                    /**  this.vOrderedItems.addItem({
                     itemId: itemSel,
                     itemName: rowObj.itemName,
                     quantity: selQty,
                     orderedPrice: rowObj.orderedPrice,
                     desc: rowObj.itemDesc
                     });
                     this.vOrderedItems.removeItem(row);
                     **/
                    this.vOrderedItems.getItem(row).setValue("quantity", selQty);


                    break;
                }

            }


        }
    },
    selectOrderItem: function(selItemId, itemRowNum) {
        //alert(selItemId + 'hiii' + itemRowNum);
        var itemExist = false;
        if (this.vOrderedItems) {
            for (var i = 0; i < this.vOrderedItems.getCount(); i++) {
                var oItem = this.vOrderedItems.getItem(i).data;


                var itemSel = oItem.itemId;
                if (itemSel === selItemId) {
                    itemExist = true;
                    this.vOrderedItems.removeItem(i);
                }
            }
        }


        if (!itemExist) {

            var rowData = this.listVendorItems.getItemData(itemRowNum);
            //console.error("rowData==" + rowData);
            var price = (1 * rowData.price);
            //console.error(rowData.itemDesc);
            this.vOrderedItems.addItem({
                itemId: selItemId,
                itemName: rowData.itemName,
                quantity: 1,
                orderedPrice: price,
                desc: rowData.itemDesc
            });

        }

        //
    },

    selectItemQuantity: function(selItemId, selQuantity) {

        console.error("selItemId=" + selItemId + ";selQuantity=" + selQuantity);
        for (var i = 0; i < this.orderQtyArr.length; i++) {
            var itemQty = this.orderQtyArr[i];
            var itemId = itemQty.substr(0, itemQty.indexOf("#"));
            var selQty = itemQty.substr(itemQty.indexOf("#") + 1);

            if (itemId == selItemId) {
                this.orderQtyArr.splice(i, 1);
                break;
            }

        }
        this.orderQtyArr.push(selItemId + "#" + selQuantity);

    },

    listVendorItemsItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {



        var display = "<div class='MobileRow' style='font-size:15px'><b>" + rowObj.itemName + "</b></div>" + "<div class='MobileRow'> </div>" + "<div class='MobileRow'>Also called: " + rowObj.otherNames + "</div>" + "<div class='MobileRow'>Details: " + rowObj.itemDesc + "</div>";

        return display;
    },
    listVendorItemsItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var selItemName = rowObj.itemName;
        //alert(selItemName);
        //    return '<INPUT TYPE=CHECKBOX NAME="defaultView" CHECKED onchange=main.pcContent.page.pcDashboard.page.pdlgViewPreferenceOptions.page.pdManagePreference.page.setDefaultUserPreference(' + inValue + ',' + rowId + ')><P>';
        //var chkBoxStr = '<div class="MobileRow">Order</div>' + ' <INPUT TYPE="CHECKBOX" NAME=chk' + inValue + '" id="chk' + inValue + '" onchange="main.pcCustomerContent.page.pcItemList.page.selectOrderItem('+rowId+ ',' + inValue + ');" ></input>';
        var chkBoxStr = '<div class="MobileRow" style="font-size:13px">Order</div> <div class="MobileRow" style="font-size:13px"> <INPUT TYPE=CHECKBOX height="20px" NAME="defaultView" onchange=main.pcCustomerContent.page.pcItemList.page.selectOrderItem(' + inValue + ',' + rowId + ') > </div>';

        return chkBoxStr;
    },
    listVendorItemsQuantityFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var items = '<div class="MobileRow" style="font-size:12px">Quantity </div> <div class="MobileRow"> <select name="Quantity" onchange="main.pcCustomerContent.page.pcItemList.page.selectItemQuantity(' + rowObj.itemId + ',this.options[this.selectedIndex].value);"> ' + '<option value="1">1</option>' + '<option value="2">2</option>' + '<option value="2.5">2.5</option>' + '<option value="5">5</option>' + '<option value="6">6</option>' + '<option value="7">7</option>' +

        '</select> </div>';
        return items;
    },
    listVendorItemsItemDescFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var commentBox = "<div class='MobileRow'>Comment:</div>" + "<input type='text' name='comment'/>";

        return commentBox;
    },

    list1ItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>Item Name: " + rowObj.desc + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>OrderedPrice: " + rowObj.orderedPrice + "</div>";


        return display;
    },
    svPlaceOrderSuccess: function(inSender, inDeprecated) {

    },
    listVendorItemsImageUrlFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        try {
            var file = "services/FileUploadDownload.download?method=downloadFile&fileName=" + inValue;
            return '<img  style="height: 80px;width: 80px;" src="' + file + '" />';
        } catch (e) {
            console.error('ERROR IN dojoGrid2PicurlFormat: ' + e);
        }
    },

    listFinalOrderItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>Item Name: " + rowObj.itemName + "</div>" + "<div class='MobileRowTitle'>Item Desc: " + rowObj.desc + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>OrderedPrice: " + rowObj.orderedPrice + "</div>";
        return display;
    },
    svCompleteOrderSuccess1: function(inSender, inDeprecated) {
        var result = inSender.data.dataValue;
        var orderId = result.split("#")[1];
        orderId=orderId.split("and")[0].trim();
        
        this.svMailService.input.setValue("orderId", orderId);
        this.svMailService.update();
        //  alert(inSender.data.dataValue);
        this.ncOrderResult.input.text=inSender.data.dataValue;
        this.ncOrderResult.update();
        this.vOrderedItems.clearData();
        wm.Page.getPage("DCustomerHomePg").ncServices.update();
        wm.Page.getPage("DCustomerHomePg").panelOrderResultDisplay.setShowing(true);
        wm.Page.getPage("DCustomerHomePg").labelOrderResult.setCaption(inSender.data.dataValue);



    },
    listVendorItemsPriceFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRow' style='font-size:13px'><font color='red'>Price </font></div>  <div class='MobileRow' style='font-size:13px'><b> <font color='red'>" + rowObj.price + " Rs</font></b></div>";

        return display;

    },


    _end: 0
});